home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7934 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Large Number Arithmetic
  5. Date: 29 Feb 1996 19:04:07 GMT
  6. Organization: OpenVision
  7. Message-ID: <4h4tb7$qlp@spanky.pls.ov.com>
  8. References: <4gu61k$52@vnetnews.value.net>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 52@vnetnews.value.net, Adonis Ioannou <adonis@value.net> writes:
  13. >Your help will be greatly appreciated...
  14. >
  15. >I am trying to create a simple calculator, which supports + - / * %, using the 
  16. >following input from the command line:-
  17. >
  18. >                 nnn + nnn where  nnn is an integer up to 40 digits long.
  19. >
  20. >The output should also be of the same format(no scientific notations)
  21. >
  22. >I have tried to read the values using 
  23. >
  24. >                  scanf("%Lf", &x) where x is a long double(so that I can store these 
  25. >                                                            large numbers),
  26. >but after I enter more than 20 digits, I start to lose some accuracy.
  27. >
  28. >I also tried to read the input into character arrays, and then convert the string 
  29. >using _atold(), but had the same problem. Can anyone give me any clues or suggestions?
  30. >
  31. >I am using VisualC++ 1.52, under WIN95.
  32. >
  33. >Here is an example of what I mean:-
  34. >
  35. >#include <stdio.h>
  36. >#include <stdlib.h>
  37. >
  38. >void main(void)
  39. >{      
  40. >    long double x;     
  41. >                 
  42. >    scanf("%Lf", &x);
  43. >    printf("%Lf", x);
  44. >}
  45. >
  46. >If I enter 12345678901234567890 I get the same result.
  47. >If I enter 123456789012345678901 I get 123456789012345678904
  48. >
  49. >Thanks
  50. >
  51.  
  52.  
  53.  
  54. It doesn't matter what machine you are on, a double on a given machine can
  55. only hold so many significant digits.  If you try to enter more significant
  56. digits, the machine has no way to store them.  If you wish to use larger
  57. numbers of digits, then you must emulate larger data storage types than the
  58. machine can handle.  Emulate means that now, your program is the calculator
  59. and not the machine.
  60.  
  61.             Fletcher.Glenn@ov.com
  62.  
  63.